var oBox = document.querySelector(".buybox");
var data = [{
    id: 1,
    name: "GIGABYTE 技嘉 NVIDIA GeForce RTX3060Ti 显卡 GDDR6 8GB",
    price: 8499.89,
    src: "https://images-cn.ssl-images-amazon.cn/images/I/61Obieq3R1L._AC_SX679_.jpg"
}]
//点击加入购物车
oBox.addEventListener("click", function (evt) {
    var e = evt || window.event;
    var ele = e.target;
    if (!(ele.nodeName == 'A' && ele.className == 'add')) {
        return;
    }
    //1.得到id
    var tempId = ele.getAttribute("data-id");
    var current = null;
    data.forEach(function (item) {
        if (item.id == tempId) {
            current = item;
        }
    })
    //2.获取本地存储的数据
    var cartList = JSON.parse(localStorage.getItem("cart") || '[]');
    //3.是否购买过?
    var isAdd = false; //没有购买过 默认 true
    //4.变量 cartList 与每一项的id与tempId进行比较
    cartList.forEach(function (item) {
        if (item.id == current.id) {
            item.number = Number(item.number) + 1;
        }
        return isAdd = true;
    })
    //5.没有购买过
    if (isAdd == false) {
        current.number = 1;
        cartList.push(current);
    }
    //6.把cartList覆盖原来的数据
    localStorage.setItem("cart", JSON.stringify(cartList));
    alert("加入成功,请前往购物车查看!");
})